<?php

class survey extends CI_Controller{
       

     public function __construct() {

        parent::__construct();

        // loads the survey model
        $this->load->model('SurveyModel');
        $this->load->model('zipcodemodel');
        $this->load->library('session');
    }
    public function index() {
       $id  = $_GET['id'];
       $data['id'] = $id;
       $this->load->view('survey_view', $data);
       
    }
    public function addSurvey(){
          //unset($this->session->userdata); 
          $data['idsp1'] = $this->input->post('idsp1');
          $data['idsp2'] = $this->input->post('idsp2');
          $data['cources'] = $this->input->post('cource');
          $data['workplace'] = $this->input->post('workplace');
          $data['department'] = $this->input->post('department');
          $data['name1'] = $this->input->post('name1');
          $data['name2'] = $this->input->post('name2');
          $data['namekana1'] = $this->input->post('namekana1');
          $data['namekana2'] = $this->input->post('namekana2');
          $data['sex'] =  $this->input->post('sex');
          $data['email'] = $this->input->post('email');
          
          $data['zip'] = $this->input->post('zip1').'-'.$this->input->post('zip2');
          $data['area'] =$this->input->post('setting_area2_id');
          $data['city'] =$this->input->post('city');
          $data['town'] =$this->input->post('town');
          $data['numphone'] = $this->input->post('tel1').$this->input->post('tel2').$this->input->post('tel3');;
          $data['addressbuy'] =$this->input->post('addressbuy');
          $id  = $_GET['id'];
          $data['id'] = $id;
          /*
          $data['q1']=  !isset($_POST["q1"]) ? null : $this->getQ1(implode("",$_POST["q1"]));
          $data['q2']=  !isset($_POST["q2"]) ? null : $this->getQ2(implode("",$_POST["q2"]));
         
          if($id==1){
               $data['q31']=  !isset($_POST["q31"]) ?  null : $this->getQ3_1(implode("",$_POST["q31"]));
               $data['q41']=  !isset($_POST["q41"]) ? null : $this->getQ4_1(implode("",$_POST["q41"]));
          }
          else if($id==2){
              $data['q32']=  !isset($_POST["q32"]) ? null :$this->getQ3_2(implode("",$_POST["q32"]));
              $data['q42']=  !isset($_POST["q42"]) ? null :$this->getQ4_2(implode("",$_POST["q42"]));
              $data['q5']=  !isset($_POST["q5"]) ? null :$this->getQ5(implode("",$_POST["q5"]));
              $data['q6']=  !isset($_POST["q6"]) ? null :$this->getQ6(implode("",$_POST["q6"]));
              $data['q7']=  !isset($_POST["q7"]) ? null :$this->getQ7(implode("",$_POST["q7"]));
              $data['q8']=  !isset($_POST["q8"]) ? null :$this->getQ8(implode("",$_POST["q8"]));
              $data['q9']=  !isset($_POST["q9[]"]) ? null :$this->getQ9(implode("",$_POST["q9"]));
          }
           
           */
          $data['q1']=  !isset($_POST["q1"]) ? null : $this->getQ1(implode("-",$_POST["q1"]));
          $data['q2']=  !isset($_POST["q2"]) ? null : $this->getQ2(implode("-",$_POST["q2"]));
         
          if($id < 7){
               $data['q31']=  !isset($_POST["q31"]) ?  null : $this->getQ3_1(implode("-",$_POST["q31"]));
               $data['q41']=  !isset($_POST["q41"]) ? null : $this->getQ4_1(implode("-",$_POST["q41"]));
          }
          else {
              $data['q32']=  !isset($_POST["q32"]) ? null :$this->getQ3_2(implode("-",$_POST["q32"]));
              $data['q42']=  !isset($_POST["q42"]) ? null :$this->getQ4_2(implode("-",$_POST["q42"]));
              $data['q5']=  !isset($_POST["q5"]) ? null :$this->getQ5(implode("-",$_POST["q5"]));
              $data['q6']=  !isset($_POST["q6"]) ? null :$this->getQ6(implode("-",$_POST["q6"]));
              $data['q7']=  !isset($_POST["q7"]) ? null :$this->getQ7(implode("-",$_POST["q7"]));
              $data['q8']=  !isset($_POST["q8"]) ? null :$this->getQ8(implode("-",$_POST["q8"]));
              $data['q9']=  !isset($_POST["q9"]) ? null :$this->getQ9(implode("-",$_POST["q9"]));
          }
        $data['q10'] = $this->input->post('q10');
         $data['areaStr'] = $this->getArea($data['area']);
        $this->setDataSession($data);
        $this->load->view('survey_confim', $data);
    }
    public function addSurveyFormSubmission() {
	//$params['created'] = $this->input->post('created');
        $params['idsp1'] = $this->session->userdata('idsp1');
        $params['idsp2'] = $this->session->userdata('idsp2');
        $params['courses'] = $this->session->userdata('cources');
        $params['name1'] = $this->session->userdata('name1');
        $params['name2'] = $this->session->userdata('name2');
        $params['namekana1'] = $this->session->userdata('namekana1');
        $params['namekana2'] = $this->session->userdata('namekana2');
        $params['workplace'] = $this->session->userdata('workplace');
        $params['department'] = $this->session->userdata('department');
        $params['email'] = $this->session->userdata('email');
        $params['zip'] = $this->session->userdata('zip');
        $params['state'] = $this->session->userdata('area');
        $params['city'] = $this->session->userdata('city');
        $params['address'] = $this->session->userdata('town');
        $params['gender'] = $this->session->userdata('sex');
        $params['phonenumber'] = $this->session->userdata('numphone');
        $params['supplier'] = $this->session->userdata('addressbuy');
        $params['idsurvey'] = $this->session->userdata('id');  
        if($this->session->userdata('id') < 7){
        $params['q1'] = $this->session->userdata('q1');
        $params['q2'] = $this->session->userdata('q2');
        $params['q3'] = $this->session->userdata('q31');
        $params['q4'] = $this->session->userdata('q41');
        $params['q5'] = "";
        $params['q6'] = "";
        $params['q7'] = "";
        $params['q8'] = "";
        $params['q9'] = "";
      
        }
        else{
        $params['q1'] = $this->session->userdata('q1');
        $params['q2'] = $this->session->userdata('q2');
        $params['q3'] = $this->session->userdata('q32');
        $params['q4'] = $this->session->userdata('q42');
        $params['q5'] = $this->session->userdata('q5');
        $params['q6'] = $this->session->userdata('q6');
        $params['q7'] = $this->session->userdata('q7');
        $params['q8'] = $this->session->userdata('q8');
        $params['q9'] = $this->session->userdata('q9');
        }
        $params['q10'] =$this->session->userdata('q10');

        $addSurvey = $this->SurveyModel->insertSurvey($params);

        if ($addSurvey) {
            $this->session->set_flashdata('success_msg', 'Your category has been added successfully');
            unset($this->session->userdata); 
            $this->load->view('survey_result');
        } else {
            $this->session->set_flashdata('error_msg', 'Please try again adding a category');
            redirect('survey/addSurvey');
        }
    }
    public function getZipCode(){
        $data="";
        if($_POST['zipcode'] == "")
        {
            $message = "You can't send empty text";
        }
        else
        {
            $zip = $_POST['zipcode'];
            $data =  $this->zipcodemodel->getAddress($zip);
            //$row = $query->result_array();
            //$data += $row->setting_area2_id +','+$row->cityName +','+ $row->townName;
        }
        echo $data;
    }
    
    public function getQ1($pQ1){
        $arr = explode('-',$pQ1);
        $arr_q1 ="";
        $Q1 = array(
	1=>'キムワイプS-200',
	2=>'キムワイプM-150',
	3=>'キムワイプL-100',
	4=>'JKワイパー150-S',
	5=>'JKワイパー100-S',
	6=>'ケイドライ132-S',
	7=>'キムタオル4つ折り（茶色）',
	8=>'キムタオルホワイト4つ折り'
        );
        for($i = 0; $i< count($arr); $i++){
           // array_push($arr_q1, $Q1[$arr[$i]]);
            $arr_q1 .= $Q1[$arr[$i]].'|';
        }
        return $arr_q1;
    }
    public function getQ2($pQ2){
        $arr = explode('-',$pQ2);
        $arr_q2 ="";// array();
        $Q2 = array(
	1=>'１～２ケース',
	2=>'３～４ケース',
	3=>'５～９ケース',
	4=>'１０～２９ケース',
	5=>'３０ケース以上'
        );
        for($i = 0; $i< count($arr); $i++){
            //array_push($arr_q2, $Q2[$arr[$i]]);
            $arr_q2 .= $Q2[$arr[$i]].'|';
        }
        return $arr_q2;
    }
    public function getQ3_1($pQ3_1){
        $arr = explode('-',$pQ3_1);
        $arr_q3_1 ="";//  array();
        $Q3_1 = array(
	1=>'官公庁研究機関',
	2=>'大学',
	3=>'各種学校',
	4=>'民間企業研機関',
	5=>'工場',
	6=>'病院',
	99=>'その他'
        );
        for($i = 0; $i< count($arr); $i++){
           
            if($arr[$i]==99){
               // $text = $Q3_1[$arr[$i]].':'.$this->input->post('q31text');
                //array_push($arr_q3_1, $text);
                $arr_q3_1 .=$Q3_1[$arr[$i]].'|'.$this->input->post('q31text').'|';
            }
            else {
                 $arr_q3_1 .= $Q3_1[$arr[$i]].'|';
            }
            //else array_push($arr_q3_1, $Q3_1[$arr[$i]]);
        }
        return $arr_q3_1;
    }
    public function getQ4_1($pQ4_1){
        $arr = explode('-',$pQ4_1);
        $arr_q4_1 ="";// array();
        $Q4_1 = array(
	1=>'作業台のふきとり',
	2=>'実験器具のふきとり',
	3=>'油のふきとり',
	4=>'薬液のふきとり',
	5=>'水のふきとり',
	6=>'機材・器具部品のふきあげ',
	7=>'傷つきやすい物のふきあげ',
	8=>'レンズのふきあげ',
	9=>'検査工程',
	10=>'布・ウエスの代用'
        );
        for($i = 0; $i< count($arr); $i++){
            //array_push($arr_q4_1, $Q4_1[$arr[$i]]);
            $arr_q4_1 .= $Q4_1[$arr[$i]].'|';
        }
        return $arr_q4_1;
    }
    public function getQ3_2($pQ3_2){
        $arr = explode('-',$pQ3_2);
        $arr_q3_2 ="";//  array();
        $Q3_2 = array(
	1=>'一般工場',
	2=>'印刷',
	3=>'食品',
	4=>'医薬品',
	5=>'化粧品',
	6=>'電気精密',
	7=>'メンテナンス',
	8=>'病院',
	9=>'介護',
	10=>'車両整備',
	11=>'塗装',
	12=>'研究機関',
	13=>'学校',
	99=>'その他'
        );
        for($i = 0; $i< count($arr); $i++){
           
            if($arr[$i]==99){
               // $text = $Q3_1[$arr[$i]].':'.$this->input->post('q31text');
                //array_push($arr_q3_1, $text);
                $arr_q3_2 .=$Q3_2[$arr[$i]].'|'.$this->input->post('q32text').'|';
            }
            else {
                 $arr_q3_2 .= $Q3_2[$arr[$i]].'|';
            }
            //else array_push($arr_q3_1, $Q3_1[$arr[$i]]);
        }
        /* for($i = 0; $i< count($arr); $i++){
            if($arr[$i]==99){
                $text = $Q3_2[$arr[$i]].':'.$this->input->post('q32text');
                array_push($arr_q3_2, $text);
            }
            else array_push($arr_q3_2, $Q3_2[$arr[$i]]);
        }*/
        return $arr_q3_2;
    }
    public function getQ4_2($pQ4_2){
        $arr =explode('-',$pQ4_2);
        $arr_q4_2 ="";//  array();
        $Q4_2 = array(
         1=>'油',
         2=>'薬液',
         3=>'インク',
         4=>'塗料',

         99=>'その他'

        );
        for($i = 0; $i< count($arr); $i++){
            
            if($arr[$i]==99){
               // $text = $Q3_1[$arr[$i]].':'.$this->input->post('q31text');
                //array_push($arr_q3_1, $text);
                $arr_q4_2 .=$Q4_2[$arr[$i]].'|'.$this->input->post('q42text').'|';
            }
            else {
                $arr_q4_2 .= $Q4_2[$arr[$i]].'|';
            }
            //else array_push($arr_q3_1, $Q3_1[$arr[$i]]);
        }/*
        for($i = 0; $i< count($arr); $i++){
            if($arr[$i]==99){
                $text = $Q4_2[$arr[$i]].':'.$this->input->post('q42text');
                array_push($arr_q4_2, $text);
            }
            else array_push($arr_q4_2, $Q4_2[$arr[$i]]);
        }*/
        return $arr_q4_2;
    }
     public function getQ5($pQ5){
        $arr = explode('-',$pQ5);
        $arr_q5 ="";//  array();
         $Q5 = array(
         1=>'汚れの拭き取り',
         2=>'脱脂洗浄',
         3=>'除菌・消毒清掃',
         4=>'敷きもの用途',
         5=>'包む用途',
         99=>'その他'
        );
        for($i = 0; $i< count($arr); $i++){
          
            if($arr[$i]==99){
               // $text = $Q3_1[$arr[$i]].':'.$this->input->post('q31text');
                //array_push($arr_q3_1, $text);
                $arr_q5 .=$Q5[$arr[$i]].'|'.$this->input->post('q5text').'|';
            }
            else{
                 $arr_q5 .= $Q5[$arr[$i]].'|';
            }
            //else array_push($arr_q3_1, $Q3_1[$arr[$i]]);
        }/*
        for($i = 0; $i< count($arr); $i++){
            if($arr[$i]==99){
                $text = $Q5[$arr[$i]].':'.$this->input->post('q5text');
                array_push($arr_q5, $text);
            }
            else array_push($arr_q5, $Q5[$arr[$i]]);
        }*/
        return $arr_q5;
    }
     public function getQ6($pQ6){
        $arr =explode('-',$pQ6);
        $arr_q6 ="";//  array();
        $Q6 = array(
	1=>'乾いた状態',
	2=>'濡らした状態'
        );
        for($i = 0; $i< count($arr); $i++){
            //array_push($arr_q6, $Q6[$arr[$i]]);
            $arr_q6 .=$Q6[$arr[$i]];
        }
        return $arr_q6;
    }
     public function getQ7($pQ7){
        $arr = explode('-',$pQ7);
        $arr_q7 ="";//  array();
        $Q7 = array(
	1=>'水',
	2=>'アルコール',
	3=>'溶剤',
	99=>'その他'
        );
        for($i = 0; $i< count($arr); $i++){
            
            if($arr[$i]==99){
               // $text = $Q3_1[$arr[$i]].':'.$this->input->post('q31text');
                //array_push($arr_q3_1, $text);
                $arr_q7 .=$Q7[$arr[$i]].'|'.$this->input->post('q7text').'|';
            }else{
                $arr_q7 .= $Q7[$arr[$i]].'|';
            }
            //else array_push($arr_q3_1, $Q3_1[$arr[$i]]);
        }/*
        for($i = 0; $i< count($arr); $i++){
            if($arr[$i]==99){
                $text = $Q5[$arr[$i]].':'.$this->input->post('q7text');
                array_push($arr_q7, $text);
            }
            else array_push($arr_q7, $Q7[$arr[$i]]);
        }*/
        return $arr_q7;
    }
     public function getQ8($pQ8){
        $arr = explode('-',$pQ8);
        $arr_q8 ="";//  array();
        $Q8 = array(
	1=>'併用している',
	2=>'併用していない'
        );
        for($i = 0; $i< count($arr); $i++){
            //array_push($arr_q8, $Q8[$arr[$i]]);
            $arr_q8 .= $Q8[$arr[$i]].'|';
        }
        return $arr_q8;
    }
     public function getQ9($pQ9){
        $arr = explode('-',$pQ9);
        $arr_q9 ="";//  array();
        $Q9 = array(
	1=>'メリヤスウエス',
	2=>'白ウエス',
	3=>'タオルウエス',
	4=>'色メリヤスウエス',
	5=>'色・シマウエス',
	6=>'レンタルウエス',
	99=>'その他'
        );
        for($i = 0; $i< count($arr); $i++){
           
            if($arr[$i]==99){
               // $text = $Q3_1[$arr[$i]].':'.$this->input->post('q31text');
                //array_push($arr_q3_1, $text);
                $arr_q9 .=$Q9[$arr[$i]].'|'.$this->input->post('q9text').'|';
            }
            else{
                 $arr_q9 .= $Q9[$arr[$i]].'|';
            }
            //else array_push($arr_q3_1, $Q3_1[$arr[$i]]);
        }/*
         for($i = 0; $i< count($arr); $i++){
            if($arr[$i]==99){
                $text = $Q9[$arr[$i]].':'.$this->input->post('q9text');
                array_push($arr_q9, $text);
            }
            else array_push($arr_q9, $Q9[$arr[$i]]);
        }*/
        return $arr_q9;
    }
    public function setDataSession($data){
        if($data['id'] < 7){
        $this->session->set_userdata(array(
                            'idsp1'       => $data['idsp1'],
                            'idsp2'      =>  $data['idsp2'],
                            'cources'       =>  $data['cources'],
                            'workplace'          =>  $data['workplace'],
                            'department'        =>  $data['department'],
                            'name1'        =>  $data['name1'],
                            'name2'        =>  $data['name2'],
                            'namekana1'        =>  $data['namekana1'],
                            'namekana2'        =>  $data['namekana2'],
                            'sex'        =>  $data['sex'],
                            'email'        =>  $data['email'],
                            'zip'        =>  $data['zip'],
                            'area'        =>  $data['area'],
                            'city'        =>  $data['city'],
                            'town'        =>  $data['town'],
                            'numphone'        =>  $data['numphone'],
                            'addressbuy'        =>  $data['addressbuy'],
                            'id'        =>  $data['id'],
                            'q1'        =>  $data['q1'],
                            'q2'        =>  $data['q2'],
                            'q31'        => $data['q31'],
                            'q41'        =>  $data['q41'],
                            'q10'       => $data['q10']
                            
                    ));
        }else {
            $this->session->set_userdata(array(
                            'idsp1'       => $data['idsp1'],
                            'idsp2'      =>  $data['idsp2'],
                            'cources'       =>  $data['cources'],
                            'workplace'          =>  $data['workplace'],
                            'department'        =>  $data['department'],
                            'name1'        =>  $data['name1'],
                            'name2'        =>  $data['name2'],
                            'namekana1'        =>  $data['namekana1'],
                            'namekana2'        =>  $data['namekana2'],
                            'sex'        =>  $data['sex'],
                            'email'        =>  $data['email'],
                            'zip'        =>  $data['zip'],
                            'area'        =>  $data['area'],
                            'city'        =>  $data['city'],
                            'town'        =>  $data['town'],
                            'numphone'        =>  $data['numphone'],
                            'addressbuy'        =>  $data['addressbuy'],
                            'id'        =>  $data['id'],
                            'q1'        =>  $data['q1'],
                            'q2'        =>  $data['q2'],
                            'q32'        => $data['q32'],
                            'q42'        =>  $data['q42'],
                            'q5'        => $data['q5'],
                            'q6'        => $data['q6'],
                            'q7'        =>$data['q7'],
                            'q8'        => $data['q8'],
                            'q9'        => $data['q9'],
                            'q10'       => $data['q10']
                    ));
        }
    }
    public function  getArea($pArea){
        $arr = array(1=>'北海道',
                    2=>'青森県',
                    3=>'岩手県',
                    4=>'宮城県',
                    5=>'秋田県',
                    6=>'山形県',
                    7=>'福島県',
                    8=>'茨城県',
                    9=>'栃木県',
                   10=>'群馬県',
                    11=>'埼玉県',
                    12=>'千葉県',
                    13=>'東京都',
                    14=>'神奈川県',
                   15=>'新潟県',
                    16=>'富山県',
                   17=>'石川県',
                    18=>'福井県',
                    19=>'山梨県',
                   20=>'長野県',
                    21=>'岐阜県',
                    22=>'静岡県',
                    23=>'愛知県',
                    24=>'三重県',
                    25=>'滋賀県',
                    26=>'京都府',
                    27=>'大阪府',
                    28=>'兵庫県',
                    29=>'奈良県',
                    30=>'和歌山県',
                    31=>'鳥取県',
                    32=>'島根県',
                    33=>'岡山県',
                   34=>'広島県',
                    35=>'山口県',
                    36=>'徳島県',
                    37=>'香川県',
                    38=>'愛媛県',
                    39=>'高知県',
                    40=>'福岡県',
                   41=>'佐賀県',
                    42=>'長崎県',
                   43=>'熊本県',
                    44=>'大分県',
                   45=>'宮崎県',
                    46=>'鹿児島県',
                    47=>'沖縄県');
       
         if($pArea!= -1)
            return $arr[$pArea];
        else return "--末選択--";
    }



}
